import matplotlib.pyplot as plt
import random
import math

# Create the samples
X = list(range(40))
y = []
for i in range(len(X)):
    y.append(20+X[i]+random.random()*20)

# linear regression evolution 
fig, axs = plt.subplots(1,4, figsize=(15,4))

# return the prediction of x data, by using kernel function
def kernel_function(b, X, y, x, figure):
    y_prediction = 0
    for i in range(len(X)):
        y_prediction += w(b, i, X, x)*y[i]
    y_prediction /= len(X)
    return y_prediction    

def w(b, i, X, x):
    denom = 0;
    for j in range(len(X)):
        denom += kernel((X[j]-x)/b)
    return (len(X)*kernel((X[i]-x)/b))/denom

def kernel(z):
    return math.exp(-(z*z)/2)/math.sqrt(2*math.pi)

def plot(fig, X, y, parameters, label):
    axs[fig].plot(X, y, parameters, label = label)
    axs[fig].legend(); axs[fig].grid(); 
    return

for b, fig in zip([7, 3, 0.8, 0.03],range(4)):
    y_prediction = []
    for x in range(len(X)):
        y_prediction.append(kernel_function(b, X, y, x, 0))
    plot(fig, X, y, 'yo', "samples")   # plot samples in figure 
    # plot the (x,y_prediction) prediction                                     
    plot(fig, X, y_prediction,"k-", 'predictions')                        
    axs[fig].set_xlabel('b= ' + '{:4.2f}'.format(b))

plt.show()
